﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.Objective;
using Jumpton.Persistent;
using Jumpton.SFA.Server.BusinessRule;

/// <summary>
/// 目标类型维护
/// Update By:Allen Jiang
/// Update Time:2008-12-22
/// Description:
///     修改目标类型维护在一个页面。
/// </summary>
public partial class Objective_ObjectiveDetailList : PageBase
{
    public string ObjectiveID = "";
    public static string keyid = "";
    
    ObjectiveManager manager = new ObjectiveManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["OID"] != null)
            ObjectiveID = Request.QueryString["OID"].ToString();
       
        if (!IsPostBack)
        {
            txtStartDate.Value = DateTime.Now.AddMonths(-1).ToString("yyyy-MM");
            txtEndDate.Value = DateTime.Now.ToString("yyyy-MM");
            ddlBind();
           
            ObjectiveBind();


            
        }
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    ///

    public void ddlBind()
    {
        ObjectTable ot= manager.SelectObjectiveList(MyStateManager.DomainID, "");
        drpObjective.DataSource = ot;
        drpObjective.DataTextField = "NAME";
        drpObjective.DataValueField = "ID";
        drpObjective.DataBind();

        drpObjective.Items.Insert(0, new ListItem("--全部--",""));

        ddl_kind.DataSource = new DictionaryItemsManager().GetItemList(DictionaryEnum.ObjectiveType, MyStateManager.DomainID);
        ddl_kind.DataTextField = "NAME";
        ddl_kind.DataValueField = "CODE";
        ddl_kind.DataBind();
        ddl_kind.Items.Insert(0, new ListItem("--请选择--", ""));
        if (ObjectiveID != "")
        {
            drpObjective.SelectedValue = ObjectiveID;
            drpObjective.Enabled = false;
        }
    }

    public void ObjectiveBind()
    {
        string where = "";

        if (txtStartDate.Value != "" && txtEndDate.Value != "")
        {
            where += " AND od.YEAR_MONTH BETWEEN "+getIntDate(txtStartDate.Value) +" AND "+getIntDate(txtEndDate.Value);
        }
        if (drpObjective.SelectedValue != "")
        {
            where += " AND od.OBJECTIVE_ID="+drpObjective.SelectedValue;
        }
        if (ddl_kind.SelectedValue != "")
        {
            where += " AND o.OBJECTIVETYPE=" + ddl_kind.SelectedValue;
        }
        ObjectTable ot = null;
        ot = manager.SelectObjectiveDetailViewList(MyStateManager.DomainID, where);
        GridPager1.DataSource = ot;
        GridPager1.GridView = GridView1;
        GridPager1.BindData();
        GridPager1.Visible = GridView1.Rows.Count > 0;

        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
        DetailsView1.DataBind();
        
    }
    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        CheckBox chk = DetailsView1.FindControl("chkValid") as CheckBox;
        TextBox txtData = DetailsView1.FindControl("txt_OBJECTIVE_DATA") as TextBox;
       
        RegularExpressionValidator rfv = DetailsView1.FindControl("RegularExpressionValidator1") as RegularExpressionValidator;
        if (rfv != null)
        {
            if (drpObjective.SelectedValue != "")
            {
                Objective o = manager.LoadObjective(int.Parse(drpObjective.SelectedValue));
                if (o != null)
                {

                    if (o.INPUTTYPE == 1)
                    {
                        rfv.ValidationExpression = @"^\d*";
                        rfv.ErrorMessage = "目标值应为整型";
                        if(txtData.Text!="")
                            txtData.Text = double.Parse(txtData.Text).ToString();
                        
                       
                    }
                    else
                    {
                        rfv.ValidationExpression = @"^(\d{1,3}(,\d\d\d)*(\.\d+)?|\d+(\.\d+)?)$";
                        rfv.ErrorMessage = "目标值应为数字";
                    }
                }
            }
        }
        
    }

    /// <summary>
    /// 保存数据方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            Objective_Details odetils = null;
           
            TextBox txt_OBJECTIVE_DATA = DetailsView1.FindControl("txt_OBJECTIVE_DATA") as TextBox;
            DropDownList ddlType = DetailsView1.FindControl("ddl_type") as DropDownList;
            odetils = manager.LoadDetail(int.Parse(GridView1.SelectedDataKey.Value.ToString()));
            if (odetils == null)
            {
                ShowMessage("保存失败！");
                return;
                   
            }

            odetils.ObjectiveData = decimal.Parse(txt_OBJECTIVE_DATA.Text);
          
            CheckBox cbxValid = DetailsView1.FindControl("chkValid") as CheckBox;
            odetils.Active = cbxValid.Checked;
            odetils.DomainID = MyStateManager.DomainID;
            odetils.LAST_UPDATER = MyStateManager.UserID.ToString();
            odetils.LAST_UPDATE_TIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);

            string LogContent = "";
            LogContent += ObjectiveID + "信息发生变动，变动后信息是：";
            
            LogContent += "目标值：" + odetils.ObjectiveData + "，";
            LogContent += "年月：" + odetils.YEARMONTH + "，";
            LogContent += "有效：" + odetils.Active;

            if (manager.Save(odetils))
            {
                Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                 Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
                 this.MyStateManager.UserID, LogContent);
                if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
                {
                    ShowMessage("保存成功");
                    ObjectiveBind();
                }
                else
                {
                    ShowMessage("修改成功", Request.RawUrl.ToString());
                   
                }
            }
            else
            {
                ShowMessage("保存失败");
            }
          
           
        }
        catch (Exception ex)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                    Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                    this.MyStateManager.UserID, "目标明细维护 - 异常:" + ex.Message);
        }
    }

   
   

  
    /// <summary>
    /// 新增方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
     
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        ObjectiveBind();
    }

    public string getObjectType(string value)
    {
        string type = "";
        try
        {
                type = new DictionaryItemsManager().Load(DictionaryEnum.ObjectiveType, MyStateManager.DomainID,value).Name;
        }
        catch { }
        return type;
    }

    

   

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("ObjectiveDetailMaintain.aspx?OID=" + ObjectiveID+"&KEYID="+keyid);

    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        ObjectiveBind();
    }

    public string getIntDate(string value)
    {
        return value.Replace("-", "").Replace("/", "");
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl_OBJECTIVEDATA = e.Row.FindControl("lbl_OBJECTIVEDATA") as Label;
            Label lbl_ACTUALDATA = e.Row.FindControl("lbl_ACTUALDATA") as Label;
            Label lbl_ACTUALDATATODAY = e.Row.FindControl("lbl_ACTUALDATATODAY") as Label;
            LinkButton lb = e.Row.FindControl("LinkButton2") as LinkButton;
            if (lb != null)
            {
                if (int.Parse(lb.CommandArgument)<int.Parse(DateTime.Now.ToString("yyyyMM")))
                {
                    lb.Enabled = false;
                   
                }
            }

            ObjectiveDetailView o = (e.Row.DataItem as ObjectiveDetailView);
            if (o != null)
            {
                if (o.INPUTTYPE == 1)
                {
                    lbl_OBJECTIVEDATA.Text = double.Parse(lbl_OBJECTIVEDATA.Text).ToString();
                    lbl_ACTUALDATA.Text = double.Parse(lbl_ACTUALDATA.Text).ToString();
                    lbl_ACTUALDATATODAY.Text = double.Parse(lbl_ACTUALDATATODAY.Text).ToString();
                }
            }
        }

        
        
    }
    public string getObjectName(string keyid,string objid)
    {
        string name = "";
        switch (keyid)
        {
            case "1":
                name = new PersonManager().LoadByUserID(decimal.Parse(objid)).Name;
                break;
        }

        return name;
    }

    public string getInputType(string value)
    {
        string name = "";
        try
        {
            name=new DictionaryItemsManager().Load(DictionaryEnum.ObjectiveInputType, MyStateManager.DomainID, value).Name;
        }
        catch { }
        return name;

    }
}
